<?php
$tmp =
<<<EOF
<!--{template /module/admin/view/head}-->
<div id="left_layout">
	<div id="main_content" class="container-fluid">
		<!--{template /module/admin/view/head_nav}-->
		<div class="row-fluid">
			<div class="well widget">
				<div class="widget-header">
					<h3 class="title">{$table[CNAME]}列表</h3>
				</div>
				<div class="widget-content">
					<div class="action-wrapper">
						<div class="pull-left">
EOF;

$code .= $tmp;

if($table['CODE']['STATUS'] || $table['CODE']['ISAUDIT']){
	$tmp = 
<<<EOF

							<a href="javascript:void(null);" id="btn_disable_list" class="btn btn-small" {\$dispatches[operations][audit]}>{lang admin.operation.disable}</a>
							<a href="javascript:void(null);" id="btn_enable_list" class="btn btn-small" {\$dispatches[operations][audit]}>{lang admin.operation.enable}</a>
EOF;
	
	$code .= $tmp;
}

$header_tips = '';
if($table['DETAIL_KEY']) $header_tips = "点击“{$table[DETAIL_NAME]}”查看详细信息。";

$tmp = 
<<<EOF

							<a href="javascript:void(null);" id="btn_delete_list" class="btn btn-small" {\$dispatches[operations][delete]}>{lang admin.operation.delete}</a>
							<a href="javascript:void(null);" id="btn_export" class="btn btn-small" {\$dispatches[operations][export]}>{lang admin.operation.export}</a>
							<a href="{\$ADMIN_SCRIPT}/{$table[IDENTITY]}/_add" {\$dispatches[operations][add]} class="btn btn-small">添加{$table[CNAME]}</a>
							<span class="help-inline">{$header_tips}</span>
						</div>
					</div>
					
					<div id="div_extend_search" {\$dispatches[operations][search]}>
						<hr/>
						<form id="frm_extend_search" name="frmExtendSearch" action="{\$ADMIN_SCRIPT}/{$table[IDENTITY]}&page={\$_var[page]}&psize={\$_var[psize]}" method="post">
						<input type="hidden" name="hdnSearchShow" value="1" />
						
						<table>
						<tr>
							<td>{lang admin.search.keyword}</td>
							<td>
								<label class="checkbox inline">
									<input type="text" name="txtKeyword" value="{\$_var[gp_txtKeyword]}" class="input-medium" />
								</label>
								<select name="sltType">
									<option value="0" {if \$_var['gp_sltType'] == 0}selected="selected"{/if}>=不限=</option>
EOF;

$code .= $tmp;

if(count($table['KEYWORDS']) > 0){
	foreach ($table['KEYWORDS'] as $key => $val){
		$num = $key + 1;
		$code .= "
									<option value=\"{$num}\" {if \$_var['gp_sltType'] == {$num}}selected=\"selected\"{/if}>{$val[name]}</option>";
		unset($num);
	}
}

$code .= "
					            </select>";

if($table['CODE']['GROUP']){
	$tmp = 
<<<EOF

								<select name="sltSGroupID">
							        <option value="0">=等级=</option>
							        <!--{loop \$groups \$loop \$group}-->
									<option value="{\$group[GROUPID]}"{if \$group['GROUPID'] == \$_var['gp_sltSGroupID']} selected="selected"{/if}>{\$group[CNAME]}</option>
									<!--{/loop}-->
						        </select>
EOF;
	$code .= $tmp;
}

if($table['CODE']['DISTRICT']){
	$tmp = 
<<<EOF

					            <select name="sltSProvinceId">
							        <option value="0">=省份=</option>
							        <!--{loop \$provinces \$province}-->
									<option value="{\$province[DISTRICTID]}"{if \$province['DISTRICTID'] == \$_var['gp_sltSProvinceId']} selected="selected"{/if}>{\$province[CNAME]}</option>
									<!--{/loop}-->
						        </select>
						        <select name="sltSCityId">
							        <option value="0">=市=</option>
							        <!--{loop \$cities \$city}-->
									<option value="{\$city[DISTRICTID]}"{if \$city['DISTRICTID'] == \$_var['gp_sltSCityId']} selected="selected"{/if}>{\$city[CNAME]}</option>
									<!--{/loop}-->
						        </select>
						        <select name="sltSCountyId">
							        <option value="0">=区/县=</option>
							        <!--{loop \$counties \$county}-->
									<option value="{\$county[DISTRICTID]}"{if \$county['DISTRICTID'] == \$_var['gp_sltSCountyId']} selected="selected"{/if}>{\$county[CNAME]}</option>
									<!--{/loop}-->
						        </select>
EOF;
	$code .= $tmp;
}

foreach ($table['JOINS'] as $key => $join){
	if($key == 'user' || $key == 'group' || $key == 'category' || $key == 'district') continue;
	
	$name = strtolower($key);
	$name = strtoupper(substr($name, 0, 1)).substr($name, 1);
	$name_upper = strtoupper($key);
	
	$tmp = 
<<<EOF

								<select name="sltS{$name}ID">
							        <option value="0">={$join}=</option>
							        <!--{loop \${$key}_list \$loop \${$key}}-->
									<option value="{\${$key}[{$name_upper}ID]}"{if \${$key}['{$name_upper}ID'] == \$_var['gp_sltS{$name}ID']} selected="selected"{/if}>{\${$key}[TITLE]}</option>
									<!--{/loop}-->
						        </select>
EOF;
	$code .= $tmp;
	
	unset($name);
	unset($name_upper);
}

if($table['CODE']['STATUS']){
	$tmp = 
<<<EOF

				            	<select name="sltStatus">
				            		<option value="0" {if \$_var['gp_sltStatus'] == 0}selected="selected"{/if}>=状态=</option>
						            <option value="1" {if \$_var['gp_sltStatus'] == 1}selected="selected"{/if}>已启用</option>
						            <option value="2" {if \$_var['gp_sltStatus'] == 2}selected="selected"{/if}>未启用</option>
				            	</select>
EOF;
	$code .= $tmp;
}elseif($table['CODE']['ISAUDIT']){
	$tmp = 
<<<EOF

				            	<select name="sltIsAudit">
				            		<option value="0" {if \$_var['gp_sltIsAudit'] == 0}selected="selected"{/if}>=状态=</option>
						            <option value="1" {if \$_var['gp_sltIsAudit'] == 1}selected="selected"{/if}>已审核</option>
						            <option value="2" {if \$_var['gp_sltIsAudit'] == 2}selected="selected"{/if}>未审核</option>
				            	</select>
EOF;
	$code .= $tmp;
}

$tmp = 
<<<EOF

							</td>
						</tr>
						<tr>
							<td>{lang admin.search.time}</td>
							<td>
								<input type="text" name="txtBeginDate" maxlength="16" class="input-medium input-date" value="{\$_var[gp_txtBeginDate]}" />
					            {lang admin.search.to}
					            <input type="text" name="txtEndDate" maxlength="16" class="input-medium input-date" value="{\$_var[gp_txtEndDate]}" />
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<button type="submit" class="btn btn-primary">{lang admin.search.submit.extend.submit}</button>
							</td>
						</tr>
						</table>
						</form>
					</div>
					
					<hr/>
					<form id="frm_grid" name="frmGrid" action="{\$ADMIN_SCRIPT}/{$table[IDENTITY]}&page={\$_var[page]}&psize={\$_var[psize]}" method="post">
					<table class="table table-hover">
						<thead>
							 <tr>
								<th width="30"><input type="checkbox" class="fancy" name="cbxAll"/></th>
EOF;

$code .= $tmp;

$detail_href = $detail_href_pic = '';
if($table['DETAIL_KEY']){
	$detail_href = " class=\"f14\" href=\"{\$ADMIN_SCRIPT}/{$table[IDENTITY]}/_detail&id={\${$table[IDENTITY]}[{$table[PRIMARY_KEY]}]}&page={\$_var[page]}&psize={\$_var[psize]}{\$search[querystring]}\" title=\"{\${$table[IDENTITY]}[{$table[TITLE_KEY]}]}\"";
	if($table['DETAIL_KEY'] == 'PIC') $detail_href_pic = $detail_href;
}

$code_th = $code_td = '';

$column_num = 2;
if(count($table['FILES']) > 0){
	$code_th .= "
								<th width=\"55\">图片</th>";
	$code_td .= "
								<td>
									<a{$detail_href_pic}>
										<!--{if is_array(\${$table[IDENTITY]}[FILE01])}-->
										<img src=\"{\${$table[IDENTITY]}[FILE01][0]}\" width=\"50\" style=\"width:50px;\" />
										<!--{elseif is_array(\${$table[IDENTITY]}[FILE02])}-->
										<img src=\"{\${$table[IDENTITY]}[FILE02][0]}\" width=\"50\" style=\"width:50px;\" />
										<!--{elseif is_array(\${$table[IDENTITY]}[FILE03])}-->
										<img src=\"{\${$table[IDENTITY]}[FILE03][0]}\" width=\"50\" style=\"width:50px;\" />
										<!--{else}-->
										<img src=\"static/images/no-picture.jpg\" width=\"50\" style=\"width:50px;\" />
										<!--{/if}-->
									</a>
								</td>";
	$column_num++;
}

foreach ($table['JOINS'] as $key => $join){
	if($key == 'user' || $key == 'category') continue;
	
	$name = $key == 'group' ? '等级' : $join;
	$name_upper = strtoupper($key);
	
	$code_th .= "
								<th width=\"120\">{$name}</th>";
	$code_td .= "
								<td>{\${$table[IDENTITY]}[{$name_upper}]}</td>";
	$column_num++;
	
	unset($name);
	unset($name_upper);
}

$code_th_no_width = true;

foreach ($table['COLUMNS'] as $key => $column){
	//不需要显示的字段！
	if($column['identity'] == 'AUDITTIME' 
		|| $column['identity'] == 'CITYID' 
		|| $column['identity'] == 'COUNTYID' 
		|| $column['identity'] == 'PLACE') continue;
	
	if($column['identity'] == 'PROVINCEID'){
		$code_th .= "
								<th>所在地区</th>";
		$code_td .= "
								<td>{\${$table[IDENTITY]}[PLACE]}</td>";
		continue;
	}
	
	if($column['type'] == 'text' && count($table['COLUMNS']) > 6) continue;
	if($column['identity'] == 'REMARK' && count($table['COLUMNS']) > 6) continue;
	
	if($column['type'] == 'tinyint' || $column['type'] == 'int' || $column['type'] == 'decimal'){
		if(strlen($column['name']) > 2){
			$code_th .= "
								<th width=\"60\">{$column[name]}</th>";
		}else{
			$code_th .= "
								<th width=\"40\">{$column[name]}</th>";
		}
		
		if($column['identity'] == 'GENDER'){
			$code_td .= "
								<td>{echo \${$table[IDENTITY]}['GENDER'] == 2 ? '女' : '男';}</td>";
		}elseif($column['identity'] == 'ISAUDIT'){
			$code_td .= "
								<td>{echo \${$table[IDENTITY]}['ISAUDIT'] == 1 ? '已审核' : '未审核';}</td>";
		}else{
			$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
		}
		
		$column_num++;
	}elseif($column['type'] == 'varchar'){
		if($column['identity'] == 'MOBILE' || $column['identity'] == 'CONNECTION' || $column['identity'] == 'WEIXIN'){
			$code_th .= "
								<th width=\"80\">{$column[name]}</th>";
			if($table['DETAIL_KEY'] == 'MOBILE' || $table['DETAIL_KEY'] == 'WEIXIN'){
				$code_td .= "
								<td>
									<a{$detail_href}>
										{\${$table[IDENTITY]}[{$column[identity]}]}
									</a>
								</td>";
			}else{
				$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
			}
		}elseif($column['identity'] == 'SERIAL' || $column['identity'] == 'REALNAME' || $column['identity'] == 'CONNECTER'){
			$code_th .= "
								<th width=\"60\">{$column[name]}</th>";
			if(($column['identity'] == 'SERIAL' && $table['DETAIL_KEY'] == 'SERIAL') || ($column['identity'] == 'REALNAME' && $table['DETAIL_KEY'] == 'REALNAME')){
				$code_td .= "
								<td>
									<a{$detail_href}>
										{\${$table[IDENTITY]}[{$column[identity]}]}
									</a>
								</td>";
			}else{
				$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
			}
		}elseif($column['length'] < 25){
			$code_th .= "
								<th width=\"100\">{$column[name]}</th>";
			if(($column['identity'] == 'IDNUMBER' && $table['DETAIL_KEY'] == 'IDNUMBER') || ($column['identity'] == 'CNAME' && $table['DETAIL_KEY'] == 'CNAME') || ($column['identity'] == 'TITLE' && $table['DETAIL_KEY'] == 'TITLE')){
				$code_td .= "
								<td>
									<a{$detail_href}>
										{\${$table[IDENTITY]}[{$column[identity]}]}
									</a>
								</td>";
			}else{
				$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
			}
		}elseif($column['length'] > 25 && $column['length'] < 55){
			if($column['identity'] == 'TITLE'){
				$code_th .= "
								<th width=\"180\">{$column[name]}</th>";
			}else{
				$code_th .= "
								<th width=\"120\">{$column[name]}</th>";
			}
			
			if(($column['identity'] == 'IDNUMBER' && $table['DETAIL_KEY'] == 'IDNUMBER') || ($column['identity'] == 'CNAME' && $table['DETAIL_KEY'] == 'CNAME') || ($column['identity'] == 'TITLE' && $table['DETAIL_KEY'] == 'TITLE')){
				$code_td .= "
								<td>
									<a{$detail_href}>
										{\${$table[IDENTITY]}[{$column[identity]}]}
									</a>
								</td>";
			}else{
				$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
			}
		}elseif($column['length'] > 55 && $column['length'] < 105){
			$code_th .= "
								<th width=\"180\">{$column[name]}</th>";
			if(($column['identity'] == 'IDNUMBER' && $table['DETAIL_KEY'] == 'IDNUMBER') || ($column['identity'] == 'CNAME' && $table['DETAIL_KEY'] == 'CNAME') || ($column['identity'] == 'TITLE' && $table['DETAIL_KEY'] == 'TITLE')){
				$code_td .= "
								<td>
									<a{$detail_href}>
										{\${$table[IDENTITY]}[{$column[identity]}]}
									</a>
								</td>";
			}else{
				$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
			}
		}elseif($column['length'] > 105){
			$code_th .= "
								<th>{$column[name]}</th>";
			$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
			$code_th_no_width = false;
		}
		
		$column_num++;
	}elseif($column['type'] == 'datetime'){
		$code_th .= "
								<th width=\"130\">{$column[name]}</th>";
		$code_td .= "
								<td>{\${$table[IDENTITY]}[{$column[identity]}]}</td>";
		$column_num++;
	}
}

if($code_th_no_width){
	$code_th .= "
								<th></th>";
	$code_td .= "
								<td></td>";
	$column_num++;
}

if($table['CODE']['AUDITER']){
	$code_th .= "
								<th width=\"150\">审核人/时间</th>";
	$code_td .= "
								<td>
									{\${$table[IDENTITY]}[AUDITER]}<br/>
									<span class=\"f10\">{\${$table[IDENTITY]}[AUDITTIME]}</span>
								</td>";
	$column_num++;
}elseif($table['CODE']['USER'] && !$table['CODE']['AGENT']){
	$code_th .= "
								<th width=\"150\">审核人/时间</th>";
	$code_td .= "
								<td>
									{\${$table[IDENTITY]}[USERNAME]}<br/>
									<span class=\"f10\">{\${$table[IDENTITY]}[EDITTIME]}</span>
								</td>";
	$column_num++;
}elseif($table['CODE']['USER']){
	$code_th .= "
								<th width=\"150\">操作人/时间</th>";
	$code_td .= "
								<td>
									{\${$table[IDENTITY]}[USERNAME]}<br/>
									<span class=\"f10\">{\${$table[IDENTITY]}[EDITTIME]}</span>
								</td>";
	$column_num++;
}

$code .= $code_th;

$tmp = 
<<<EOF

						        <th width="90">操作</th>
							</tr>
						</thead>
						<tbody>
							<!--{loop \${$table[IDENTITY]}_list \$_loop \${$table[IDENTITY]}}-->
							<tr class="item-row">
	        					<td>
					                <input type="checkbox" class="fancy" name="cbxItem[]" title="{\${$table[IDENTITY]}[{$table[TITLE_KEY]}]}" value="{\${$table[IDENTITY]}[{$table[PRIMARY_KEY]}]}" />
	           					</td>
EOF;

$code .= $tmp;
$code .= $code_td;

$tmp = 
<<<EOF

					            <td>
					                <a class="btn btn-mini btn-primary" href="{\$ADMIN_SCRIPT}/{$table[IDENTITY]}/_update&id={\${$table[IDENTITY]}[{$table[PRIMARY_KEY]}]}&page={\$_var[page]}&psize={\$_var[psize]}{\$search[querystring]}" {\$dispatches[operations][edit]}>{lang admin.operation.edit}</a>
				                	<a class="btn btn-mini btn-delete" title="{\${$table[IDENTITY]}[{$table[TITLE_KEY]}]}" data="{\${$table[IDENTITY]}[{$table[PRIMARY_KEY]}]}" href="javascript:void(null);" {\$dispatches[operations][delete]}>{lang admin.operation.delete}</a>		            
					            </td>
							</tr>
				            <!--{/loop}-->
				            <!--{if count(\${$table[IDENTITY]}_list) == 0}-->
					        <tr>
					            <td colspan="{$column_num}">
					                * {lang admin.operation.empty}
					            </td>
					        </tr>
					        <!--{/if}-->
						</tbody>
					</table>
					</form>
					
					<hr/>
					{\$pager}
				</div>
			</div>
		</div>
	</div>
</div>

<script>
$(function(){
	$('.fancy').uniform();
	$('.input-date').datepicker({format:'yyyy-mm-dd'});
EOF;

$code .= $tmp;

if($table['CODE']['STATUS'] || $table['CODE']['ISAUDIT']){
	if($table['CODE']['STATUS']){
		$name_enable = '启用';
		$name_disable = '禁用';
	}else{
		$name_enable = '审核通过';
		$name_disable = '不通过';
	}
	
	$tmp = 
<<<EOF

	
	$('#btn_enable_list').click(function(){
		if($("#frm_grid input[name='cbxItem[]']:checked").length > 0){
			var tempHTML = '<p>';
			$("#frm_grid input[name='cbxItem[]']:checked").each(function(){
				tempHTML += $(this).attr('title') + '； ';
			});
			
			tempHTML += '</p>';
			
			$.dialog({submit:'确认{$name_enable}', message:'<p>您确认{$name_enable}所选{$table[CNAME]}吗：</p>' + tempHTML, callback:function(){
				$('#modal_dialog .btn-primary').click(function(){
					$('#frm_grid').attr('action', "{\$ADMIN_SCRIPT}/{$table[IDENTITY]}&do=enable_list&page={\$_var[page]}&psize={\$_var[psize]}{\$search[querystring]}");
					$('#frm_grid').submit();
				});
			}});
		}else $.dialog({message:'请选择需要操作的记录！'});
	});
	
	$('#btn_disable_list').click(function(){
		if($("#frm_grid input[name='cbxItem[]']:checked").length > 0){
			var tempHTML = '<p>';
			$("#frm_grid input[name='cbxItem[]']:checked").each(function(){
				tempHTML += $(this).attr('title') + '； ';
			});
			
			tempHTML += '</p>';
			
			$.dialog({submit:'确认{$name_disable}', message:'<p>您确认{$name_disable}所选{$table[CNAME]}吗：</p>' + tempHTML, callback:function(){
				$('#modal_dialog .btn-primary').click(function(){
					$('#frm_grid').attr('action', "{\$ADMIN_SCRIPT}/{$table[IDENTITY]}&do=disable_list&page={\$_var[page]}&psize={\$_var[psize]}{\$search[querystring]}");
					$('#frm_grid').submit();
				});
			}});
		}else $.dialog({message:'请选择需要操作的记录！'});
	});
EOF;
	
	$code .= $tmp;
}

$tmp = 
<<<EOF

	
	$('#btn_delete_list').click(function(){
		if($("#frm_grid input[name='cbxItem[]']:checked").length > 0){
			var tempHTML = '<p>';
			$("#frm_grid input[name='cbxItem[]']:checked").each(function(){
				tempHTML += $(this).attr('title') + '； ';
			});
			
			tempHTML += '</p>';
			
			$.dialog({submit:'确认删除', message:'<p>您确认删除所选{$table[CNAME]}吗：</p>' + tempHTML, callback:function(){
				$('#modal_dialog .btn-primary').click(function(){
					$('#frm_grid').attr('action', "{\$ADMIN_SCRIPT}/{$table[IDENTITY]}&do=delete_list&page={\$_var[page]}&psize={\$_var[psize]}{\$search[querystring]}");
					$('#frm_grid').submit();
				});
			}});
		}else $.dialog({message:'请选择需要操作的记录！'});
	});
	
	$('#btn_export').click(function(){
		window.open('{\$ADMIN_SCRIPT}/{$table[IDENTITY]}/_excel{\$search[querystring]}');
	});
EOF;

$code .= $tmp;

if($table['CODE']['DISTRICT']){
	$tmp = 
<<<EOF

	
	$("select[name='sltSProvinceId']").change(function(){
		$("select[name='sltSCityId']").get(0).options.length = 1;
		$("select[name='sltSCountyId']").get(0).options.length = 1;
		
		if($(this).val() - 0 == 0) return;
		
		$.getJSON('{\$ADMIN_SCRIPT}/{$table[IDENTITY]}/_cities', {provinceid:$(this).val()}, function(data){
			var sltOfCity = $("select[name='sltSCityId']").get(0);
			for(var i = 0; i < data.length; i++) sltOfCity.options[i + 1] = new Option(data[i].CNAME, data[i].DISTRICTID);
		});
	});
	
	$("select[name='sltSCityId']").change(function(){
		$("select[name='sltSCountyId']").get(0).options.length = 1;
		if($(this).val() - 0 == 0) return;
		
		$.getJSON('{\$ADMIN_SCRIPT}/{$table[IDENTITY]}/_counties', {cityid:$(this).val()}, function(data){
			var sltOfCounty = $("select[name='sltSCountyId']").get(0);
			for(var i = 0; i < data.length; i++) sltOfCounty.options[i+1] = new Option(data[i].CNAME, data[i].DISTRICTID);
		});
	});
EOF;

	$code .= $tmp;
}

$tmp = 
<<<EOF

	
	$("#frm_grid input[name='cbxAll']").click(function(){
		if(this.checked){
			$('#frm_grid .item-row').addClass('warning');
			$("#frm_grid input[name='cbxItem[]']").attr('checked', true).parent().addClass('checked');
		}else{
			$('#frm_grid .item-row').removeClass('warning');
			$("#frm_grid input[name='cbxItem[]']").attr('checked', false).parent().removeAttr('class');
		}
	});
	
	$("#frm_grid input[name='cbxItem[]']").click(function(){
		if(this.checked) $(this).parent().parent().parent().parent().addClass('warning');
		else $(this).parent().parent().parent().parent().removeClass('warning');
	});
	
	$('#frm_grid .btn-delete').click(function(){
		var id = $(this).attr('data');
		var tempHTML = '<p>' + $(this).attr('title') + '； </p>';
		$.dialog({submit:'确认删除', message:'<p>您确认删除此{$table[CNAME]}吗：</p>' + tempHTML, callback:function(){
			$('#modal_dialog .btn-primary').click(function(){
				$('#frm_grid').attr('action', "{\$ADMIN_SCRIPT}/{$table[IDENTITY]}&do=delete&id="+ id +"&page={\$_var[page]}&psize={\$_var[psize]}{\$search[querystring]}");
				$('#frm_grid').submit();
			});
		}});
	});
});
</script>

<!--{template /module/admin/view/foot}-->

EOF;

$code .= $tmp;
?>